﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaiDouPhotonServer.DB.Manager;
using Photon.SocketServer;
using TaiDouCommon;
using TaiDouCommon.Model;
using TaiDouCommon.Tools;
using TaiDouPhotonServer;
using TaiDouPhotonServer.Tools;

namespace TaiDouPhotonServer.Handlers
{
    class RegisterHandler:HandlerBase
    {
        private UserManager manager;

        public RegisterHandler()
        {
            manager = new UserManager();
        }

        public override OperationCode OpCode
        {
            get
            {
                return OperationCode.Register;
            }
        }
        public override OperationResponse OnHandleMessage(OperationRequest request, ClientPeer peer)
        {
            OperationResponse response = new OperationResponse(request.OperationCode);
            User user = ParameterTool.GetParameters<User>(request.Parameters, ParameterCode.User);
            if (user == null)
            {
                response.ReturnCode = (short)ReturnCode.Error;
                response.DebugMessage = "User信息解析失败";
                return response;
            }

            User userDB = manager.GetUserByUsername(user.UserName);
            if (userDB != null)
            {
                response.ReturnCode = (short)ReturnCode.Fail;
                response.DebugMessage = "用户名重复";
            }
            else
            {
                user.Password = MD5Tool.Md5Hash(user.Password);
                manager.AddUser(user);
                response.ReturnCode = (short)ReturnCode.Succ;
            }

            return response;
        }

    }
}
